import {
  IsOptional,
  IsEnum,
  IsString,
  IsNumber,
  Min,
  Max,
} from 'class-validator';
import { ApiProperty } from '@nestjs/swagger';
import { Transform } from 'class-transformer';

export enum SortBy {
  LATEST = 'latest',
  POPULAR = 'popular',
  VIEW_COUNT = 'viewCount',
  LIKE_COUNT = 'likeCount',
}

export class ArticlesListQueryDto {
  @ApiProperty({ description: '页码', example: 1, required: false })
  @IsOptional()
  @Transform(({ value }) => parseInt(value))
  @IsNumber()
  @Min(1)
  page?: number = 1;

  @ApiProperty({ description: '每页大小', example: 20, required: false })
  @IsOptional()
  @Transform(({ value }) => parseInt(value))
  @IsNumber()
  @Min(1)
  @Max(100)
  pageSize?: number = 20;

  @ApiProperty({ description: '分类ID', required: false })
  @IsOptional()
  @Transform(({ value }) => parseInt(value))
  @IsNumber()
  categoryId?: number;

  @ApiProperty({
    description: '文章状态',
    enum: ['DRAFT', 'PUBLISHED'],
    required: false,
  })
  @IsOptional()
  @IsString()
  status?: string;

  @ApiProperty({ description: '搜索关键词', required: false })
  @IsOptional()
  @IsString()
  keyword?: string;

  @ApiProperty({ description: '排序方式', enum: SortBy, required: false })
  @IsOptional()
  @IsEnum(SortBy)
  sortBy?: SortBy = SortBy.LATEST;
}

export class ArticleListItemDto {
  @ApiProperty({ description: '文章ID' })
  id: number;

  @ApiProperty({ description: '文章标题' })
  title: string;

  @ApiProperty({ description: '文章摘要', required: false })
  summary?: string;

  @ApiProperty({ description: '封面图片URL', required: false })
  coverUrl?: string;

  @ApiProperty({ description: '浏览次数' })
  viewCount: number;

  @ApiProperty({ description: '点赞数量' })
  likeCount: number;

  @ApiProperty({ description: '评论数量' })
  commentCount: number;

  @ApiProperty({ description: '文章状态' })
  status: string;

  @ApiProperty({ description: '分类信息' })
  category: {
    id: number;
    name: string;
  };

  @ApiProperty({ description: '作者信息' })
  author: {
    id: number;
    nickname: string;
    avatar?: string;
  };

  @ApiProperty({ description: '创建时间' })
  createdAt: string;

  @ApiProperty({ description: '更新时间' })
  updatedAt: string;
}

export class ArticleDetailDto {
  @ApiProperty({ description: '文章ID' })
  id: number;

  @ApiProperty({ description: '文章标题' })
  title: string;

  @ApiProperty({ description: '文章摘要', required: false })
  summary?: string;

  @ApiProperty({ description: '文章内容' })
  content: string;

  @ApiProperty({ description: '封面图片URL', required: false })
  coverUrl?: string;

  @ApiProperty({ description: '浏览次数' })
  viewCount: number;

  @ApiProperty({ description: '点赞数量' })
  likeCount: number;

  @ApiProperty({ description: '评论数量' })
  commentCount: number;

  @ApiProperty({ description: '文章状态' })
  status: string;

  @ApiProperty({ description: '分类信息' })
  category: {
    id: number;
    name: string;
    description?: string;
  };

  @ApiProperty({ description: '作者信息' })
  author: {
    id: number;
    nickname: string;
    avatar?: string;
    bio?: string;
  };

  @ApiProperty({ description: '创建时间' })
  createdAt: string;

  @ApiProperty({ description: '更新时间' })
  updatedAt: string;
}

export class ArticlesListResponseDto {
  @ApiProperty({ description: '文章列表' })
  list: ArticleListItemDto[];

  @ApiProperty({ description: '分页信息' })
  pagination: {
    page: number;
    pageSize: number;
    total: number;
    totalPages: number;
  };
}

export class CategoryInfoDto {
  @ApiProperty({ description: '分类ID' })
  id: number;

  @ApiProperty({ description: '分类名称' })
  name: string;

  @ApiProperty({ description: '分类描述', required: false })
  description?: string;

  @ApiProperty({ description: '排序' })
  sort: number;

  @ApiProperty({ description: '父分类ID', required: false })
  parentId?: number;

  @ApiProperty({ description: '文章数量' })
  articleCount: number;
}

// 用于Swagger文档的完整API响应类型
export class ApiArticlesListResponseDto {
  @ApiProperty({ description: '响应状态码', example: 200 })
  code: number;

  @ApiProperty({ description: '响应消息', example: 'success' })
  message: string;

  @ApiProperty({ description: '响应数据' })
  data: ArticlesListResponseDto;

  @ApiProperty({ description: '时间戳' })
  timestamp: number;

  @ApiProperty({ description: '请求ID' })
  requestId: string;
}

export class ApiArticleDetailResponseDto {
  @ApiProperty({ description: '响应状态码', example: 200 })
  code: number;

  @ApiProperty({ description: '响应消息', example: 'success' })
  message: string;

  @ApiProperty({ description: '响应数据' })
  data: ArticleDetailDto;

  @ApiProperty({ description: '时间戳' })
  timestamp: number;

  @ApiProperty({ description: '请求ID' })
  requestId: string;
}
